package com.bw.z.mvp.view.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PhoneUtils;
import com.bw.z.R;
import com.bw.z.mvp.model.entity.ResultEntity;
import com.bw.z.mvp.model.entity.TokenEntity;
import com.bw.z.network.Api;
import com.bw.z.network.HttpFactory;
import com.bw.z.network.HttpType;
import com.bw.z.utils.SpUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class TestTokenActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.token_btn).setOnClickListener(this::onClick);
        findViewById(R.id.test_btn).setOnClickListener(this::onClick);
    }

    @Override
    public void onClick(View v) {
        if (R.id.token_btn == v.getId()) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            String imie = PhoneUtils.getIMEI();
            Map<String, String> map = new HashMap<>();
            map.put("imie", imie);
            LogUtils.e("imie:" + imie);
            String json = new Gson().toJson(map);
            RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);
            HttpFactory.getInstance().factory(HttpType.SIGNTYPE)
                    .getRetrofit().create(Api.class)
                    .requestToken(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<TokenEntity>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@NonNull TokenEntity tokenEntity) {
                            SpUtils.saveData("token",tokenEntity.values);
                            com.bw.z.utils.LogUtils.show(tokenEntity.message);
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            com.bw.z.utils.LogUtils.show("onError:" + e.toString());
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        } else if (R.id.test_btn == v.getId()) {
            HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                    .getRetrofit()
                    .create(Api.class)
                    .requestTest()
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<ResultEntity>() {
                        @Override
                        public void onSubscribe(@NonNull Disposable d) {

                        }

                        @Override
                        public void onNext(@NonNull ResultEntity resultEntity) {
                            LogUtils.e("onNext" + resultEntity);
                        }

                        @Override
                        public void onError(@NonNull Throwable e) {
                            LogUtils.e("onError:" + e.toString());
                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }

}